﻿using JewererBasic.BO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JewererBasic.Controllers
{
    public class TreeOrderController : Controller
    {
        MSQDBEntities db = new MSQDBEntities();

        public ActionResult CreateOrder()
        {
            ORDER_TREE tree=new ORDER_TREE(){Order_Tree_Id=-1};        
            return View(tree);
        }

        public ActionResult CreateOrderSubmit(List<ORDER_TREE_DETAIL> models)
        {
            int id;
            bool result = Tree_Order_BO.CreateNewTree(models, out id);
            return Json(new { success = result, id = id}, JsonRequestBehavior.AllowGet);            
        }

        [HttpPost]
        public ActionResult CreateOrder(ORDER_TREE model, int Order_Tree_Id)
        {
            return RedirectToAction("Detail", "TreeOrder", new { id = Order_Tree_Id });
        }

        public ActionResult Detail(int id = -1)
        {
            if (id != -1)
            {
                ORDER_TREE tree = Tree_Order_BO.GetTree(id);
                if (tree != null)
                {
                    return View(tree);
                }
                else
                {
                    return HttpNotFound("Can't find Tree Order");
                }
            }
            return HttpNotFound("Can't find Tree Order");
        }

        public ActionResult DeleteTreeDetail(int idDetail, int idOrder)
        {
            Tree_Order_BO.DeleteTreeDetail(idDetail);
            return RedirectToAction("Detail", new { id = idOrder });
        }

        public ActionResult TreeOrderDetailView(int idDetail)
        {
            ViewBag.Workers = this.db.EMPLOYEEs.ToList();

            ORDER_TREE_DETAIL detail = Tree_Order_BO.FindTreeDetail(idDetail);

            if (detail.Finish == 0) // in process of working
            {
                detail.RejectWeight = 0;
                detail.ReturnWeight = 0;
            }

            return View(detail);
        }

        [HttpPost]
        public ActionResult TreeOrderDetailView(ORDER_TREE_DETAIL model, FormCollection fc)
        {
            ViewBag.Workers = this.db.EMPLOYEEs.ToList();

            if (ModelState.IsValid)
            {
                if (fc["submit"].Equals("Save"))
                {
                    if (model.Finish == -1)
                    {
                        Tree_Order_BO.UpdateDetail(model);
                        return RedirectToAction("Detail", new { id = model.Order_Tree_Id });
                    }
                    else if (model.Finish == 0) // add weight each time
                    {
                        //Tree_Order_BO.UpdateDetail(model);
                        Tree_Order_BO.UpdateReturn(model);
                        return RedirectToAction("Detail", new { id = model.Order_Tree_Id });
                    }
                }
                else if (fc["submit"].Equals("Finish"))
                {
                    Tree_Order_BO.FinishDetail(model);
                    return RedirectToAction("TreeOrderDetailView", new { idDetail = model.Order_Tree_Detail_Id });
                }
                else if(fc["submit"].Equals("Export"))
                {
                    Tree_Order_BO.ExportDetail(model.Order_Tree_Detail_Id);
                    return RedirectToAction("Detail", new { id = model.Order_Tree_Id });
                }
            }
            return View(model);
        }

        public JsonResult IsComponentExisted(string id)
        {
            MSQDBEntities db = new MSQDBEntities();
            bool result;

            COMPONENT p = db.COMPONENTs.Find(id);
            if (p == null)
            {
                result = false;
            }
            else
            {
                result = true;
            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            return View(Tree_Order_BO.GetAllTree());
        }

        public JsonResult GetWorkerId()
        {
            List<int> ids = this.db.EMPLOYEEs.Select(m => m.Id).ToList();
            List<string> staffIds = this.db.EMPLOYEEs.Select(m => m.Staff_Id).ToList();

            string values = "";
            foreach (var item in ids)
            {
                values += item.ToString() + ";";
            }
            string texts = "";
            foreach (var item in staffIds)
            {
                texts += item + ";";
            }

            return Json(new { values, texts }, JsonRequestBehavior.AllowGet);
        }
    }
}
